<template>


    <vue-tinymce :index="$exMod.randString(10)" :setting="setting" v-model="myHtml"/>

</template>
<script>
    import Vue from "vue"
    import VueTinymce from "@packy-tang/vue-tinymce"

    Vue.use(VueTinymce)
    import {fileUpload, uploadWxmpNewsImage} from '../util/internal.api'

    export default {
        props: ['content', 'uploadType', 'height'],

        data() {
            const _vm = this;
            return {
                myHtml: "",
                setting: {
                    menubar: true,
                    toolbar: " undo redo | fullscreen | formatselect alignleft aligncenter alignright alignjustify | link unlink | numlist bullist | image media table | fontselect fontsizeselect forecolor backcolor | bold italic underline strikethrough | indent outdent | superscript subscript | removeformat |",
                    toolbar_drawer: "sliding",
                    quickbars_selection_toolbar: "removeformat | bold italic underline strikethrough | fontsizeselect forecolor backcolor",
                    plugins: "print link image media table lists fullscreen quickbars",
                    height: _vm.height,
                    language: "zh_CN",
                    images_upload_handler: function (blobInfo, success, failure) {


                        const formData = new FormData();
                        formData.append('file', blobInfo.blob());
                        if (_vm.uploadType === 'wx') {
                            uploadWxmpNewsImage(formData).then(res => {
                                if (res.code === 0) {
                                    success(res.data.url);

                                } else {
                                    failure(res.message);
                                }
                            })

                        } else {
                            //微信公众号辅材上传
                            fileUpload(formData).then(res => {
                                if (res.code === 0) {
                                    success(res.data.url);

                                } else {
                                    failure(res.message);
                                }
                            })
                        }

                    }
                },
            }
        },

        created() {
            let x = this.$exMod.isEmptyString(this.content);
            if (!x) {
                this.myHtml = this.content == null ? '' : this.content;
            }


        },

        watch: {
            myHtml(newVal) {
                this.$emit('editorHandler', newVal);
            }
        }

    }
</script>
